const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  // const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function toLogin(){
  wx.showModal({
    title: '提示',
    content: '身份验证已失效，请重新登录',
    showCancel: false,
    success: function (res) {
      if (res.confirm) {
        try {
          wx.removeStorageSync('userInfo');
          wx.reLaunch({
            url: '../login/login'
          });
        } catch (e) {
          // Do something when catch error
        }
      }
    }
  });
}

function alertBox(msg){
  wx.showModal({
    title: '提示',
    content: msg,
    showCancel: false
  });
}

var reg ={
  isNull:function(str){
    var reg = /\S/;
    var regu = new RegExp(reg);
    return regu.test(str);
  },
  isPhone: function (str) {
    var reg = /^[1][34578][0-9]{9}$/;
    var regu = new RegExp(reg);
    return regu.test(str);
  },
  isNum: function (str){
    var reg = /^[1-9][0-9]*$/;
    var regu = new RegExp(reg);
    return regu.test(str);
  }
}

module.exports = {
  formatTime: formatTime,
  toLogin:toLogin,
  alertBox:alertBox,
  reg:reg
}
